package com.prs.crm.action.ca.work;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.action.warning.AutoCodeAction;
import com.prs.crm.domain.approval.Application;
import com.prs.crm.domain.ca.work.ArticleTypes;
import com.prs.crm.domain.ca.work.DemandInventory;
import com.prs.crm.domain.ca.work.InventoryMaintain;
import com.prs.crm.domain.warning.AutoCode;
import com.prs.crm.service.approval.ApplicationService;
import com.prs.crm.service.approval.ShenQingTypeService;
import com.prs.crm.service.approval.impl.ShenQingTypeServiceImpl;
import com.prs.crm.service.ca.work.ArticleTypesService;
import com.prs.crm.service.ca.work.DemandInventoryService;
import com.prs.crm.service.ca.work.InventoryMaintainService;
import com.prs.crm.service.warning.AutoCodeService;
/**
 * @author YangHao
 * @E-Mail:450098606@qq.com
 * @qq:450098606
 * @version 创建时间：2012-4-16 10:34:01 库存维护
 */
public class InventoryMaintainAction extends BaseAction{

	private static final long serialVersionUID = -4940598646999473470L;
	private InventoryMaintain inventoryMaintain;
	private PaginationSupport pagination;
	private InventoryMaintainService inventoryMaintainService;
	private Integer[] ids;
	private Application application;
	private ApplicationService ApplicationService;
	private ShenQingTypeService shenQingTypeService = new ShenQingTypeServiceImpl();
	
	private List<DemandInventory> demandInventory;
	private DemandInventoryService demandInventoryService;
	
	private List<ArticleTypes> articleTypes;
	private ArticleTypesService articleTypesService;
	private AutoCodeService autoCodeService;
	private String codeNum;
	
	public AutoCodeService getAutoCodeService() {
		return autoCodeService;
	}
	public void setAutoCodeService(AutoCodeService autoCodeService) {
		this.autoCodeService = autoCodeService;
	}
	public String getCodeNum() {
		return codeNum;
	}
	public void setCodeNum(String codeNum) {
		this.codeNum = codeNum;
	}
	public Application getApplication() {
		return application;
	}
	public void setApplication(Application application) {
		this.application = application;
	}
	public ApplicationService getApplicationService() {
		return ApplicationService;
	}
	public void setApplicationService(ApplicationService applicationService) {
		ApplicationService = applicationService;
	}
	public ShenQingTypeService getShenQingTypeService() {
		return shenQingTypeService;
	}
	public void setShenQingTypeService(ShenQingTypeService shenQingTypeService) {
		this.shenQingTypeService = shenQingTypeService;
	}
	public List<DemandInventory> getDemandInventory() {
		return demandInventory;
	}
	public void setDemandInventory(List<DemandInventory> demandInventory) {
		this.demandInventory = demandInventory;
	}
	public DemandInventoryService getDemandInventoryService() {
		return demandInventoryService;
	}
	public void setDemandInventoryService(
			DemandInventoryService demandInventoryService) {
		this.demandInventoryService = demandInventoryService;
	}
	public List<ArticleTypes> getArticleTypes() {
		return articleTypes;
	}
	public void setArticleTypes(List<ArticleTypes> articleTypes) {
		this.articleTypes = articleTypes;
	}
	public ArticleTypesService getArticleTypesService() {
		return articleTypesService;
	}
	public void setArticleTypesService(ArticleTypesService articleTypesService) {
		this.articleTypesService = articleTypesService;
	}
	public InventoryMaintain getInventoryMaintain() {
		return inventoryMaintain;
	}
	public void setInventoryMaintain(InventoryMaintain inventoryMaintain) {
		this.inventoryMaintain = inventoryMaintain;
	}
	public PaginationSupport getPagination() {
		return pagination;
	}
	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}
	public InventoryMaintainService getInventoryMaintainService() {
		return inventoryMaintainService;
	}
	public void setInventoryMaintainService(
			InventoryMaintainService inventoryMaintainService) {
		this.inventoryMaintainService = inventoryMaintainService;
	}

	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	@Action("listInventoryMaintain")
	public String listInventoryMaintain(){
		DetachedCriteria criteria=this.getPaginationHelper().createQuery(InventoryMaintain.class);
		pagination=this.getPaginationHelper().prepare(pagination);
		this.setPagination(this.getInventoryMaintainService().listByPage(criteria, pagination));
		return SUCCESS;
	}
	@Action("editInventoryMaintain")
	public String editInventoryMaintain(){
		codeNum=getCode("WH");
		this.demandInventory=this.getDemandInventoryService().getAllDemandInventory();
		this.articleTypes=this.getArticleTypesService().getAllArticleTypes();
		return SUCCESS;
	}
	
	@Action(value = "removeInventoryMaintain", results = {
			@Result(name = "success", type = "redirect", location = "listInventoryMaintain"),
			@Result(name = "error", type = "chain", location = "listInventoryMaintain") })
	public String removeInventoryMaintain(){
		if (ids==null) {
			this.addActionError("请选择一行进行操作！");
			return ERROR;
		}
		this.getInventoryMaintainService().batchRemove(ids);
		this.setPrompt("删除成功！");
		return SUCCESS;
	}
	
	@Action(value = "saveInventoryMaintain", results = {
			@Result(name = "success", type = "redirect", location = "listInventoryMaintain"),
			@Result(name = "error", type = "chain", location = "editInventoryMaintain") })
	public String saveInventoryMaintain(){
		DemandInventory demand=new DemandInventory();
//		if(demand.getId()==null){
//			this.addActionError("请选择物品！");
//			return ERROR;
//		}else{
			if(inventoryMaintain.getIm_maintainDate()==null){
				inventoryMaintain.setIm_maintainDate(new Date());
			}
			if("".equals(inventoryMaintain.getIm_billNumber())){
				this.addActionError("单号不能为空！");
				return ERROR;
			}
			this.getInventoryMaintainService().save(inventoryMaintain);
			application = new Application();
			application.setType("一般");
			application.setAppName("[一般]"+"库存维护");
			application.setSqType(2);
			application.setSpr(0);
			application.setPerson(inventoryMaintain.getRecorder());
			application.setResign("库存维护");
			application.setInventoryMaintain(inventoryMaintain);
			//application.setShenQingType(this.getShenQingTypeService().getShenQingType("库存维护"));
			this.getApplicationService().save(application);
			return SUCCESS;
		//}
	}
	/***
	 * 根据类型进行编码的保存
	 * 
	 * @param type
	 */
	public String getCode(String type) {
	    AutoCode alist=this.getAutoCodeService().getTaskRemind(type);
	    Date dat=new Date();
	    SimpleDateFormat adf=new SimpleDateFormat("yyyyMMdd");
	    String date=adf.format(dat);
	    if(alist==null)
	    {//初始的时候调用
		    AutoCode  ac=new AutoCode();
		    ac.setCodeType(type);
		    ac.setCodeValue(type+date+"0000001");
		    ac.setAddValue("0000001");
		    this.getAutoCodeService().save(ac);	
		    return type+date+"0000001";
	    }else{
	    	String vac=alist.getAddValue();
	    	AutoCode alistA=this.getAutoCodeService().getTaskRemind(type);
	    	alistA.setCodeValue(type+date+new AutoCodeAction().addCodes(vac));
	    	alistA.setAddValue(new AutoCodeAction().addCodes(vac));
		    this.getAutoCodeService().submit(alistA);	
		    return type+date+new AutoCodeAction().addCodes(vac);
	    }
	}
}
